/**
 * 
 */
package com.lanswon.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.ArticleStatus;
import com.lanswon.common.enums.ArticleType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.ArticleVO;
import com.lanswon.service.ArticleService;

/**
 * @author lanswon
 *
 */
@Controller
@RequestMapping("/front/article")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	private Integer currentPage = 1;
	private Integer pageNum = 6;
	

	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> addArticle(HttpServletRequest request,HttpServletResponse response,ArticleVO articleVO){
		
		Map<String, Object> map = new HashMap<>();
		try {
			//数据处理操作
			if(StringUtil.isEmpty(articleVO.getArticleName())) {
				throw new RuntimeException("文章名称不能为空");
			}
			if(StringUtil.isEmpty(articleVO.getContent())) {
				throw new RuntimeException("文章内容不能为空");
			}
			//添加数据
			articleService.addArticle(articleVO);
			map.put("articleId", articleVO.getArticleId());
			map.put("success", "success");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errMsg", e.getMessage());
		}
		
		return map;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> updateArticle(HttpServletRequest request, HttpServletResponse response,ArticleVO articleVO){
		
		Map<String, Object> map = new HashMap<>();
		try {
			//数据处理
			if(articleVO.getArticleId()==null) {
				throw new RuntimeException("请正确选择需要修改的文章");
			}
			if(StringUtil.isEmpty(articleVO.getArticleName())) {
				throw new RuntimeException("文章名称不能为空");
			}
			if(StringUtil.isEmpty(articleVO.getContent())) {
				throw new RuntimeException("文章内容不能为空");
			}
			//修改
			articleService.updateArticle(articleVO);
			map.put("success", "success");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("error", "error");
		}
		
		return map;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> deleteArticle(HttpServletRequest request, HttpServletResponse response,Long articleId){
		Map<String, Object> map = new HashMap<>();
		try {
			articleService.deleteArticle(articleId);
			map.put("success", "success");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errMsg", "操作不当");
		}
		
		return map;
	}
	
	@RequestMapping("articlePage/{articleType}")
	public String articlePage(HttpServletRequest request,ModelMap modelMap,@PathVariable("articleType") Integer articleType) {
		/*Map<String, Object> accessMap = new HashMap<>();
		accessMap.put("articleType", articleType);
		accessMap.put("articleStatus", ArticleStatus.ARTICLE_STATUS_NOMAL.getCode());
		Integer articleCount = articleService.findArticleCountByCondition(accessMap);
		modelMap.addAttribute("articleType", articleType);
		modelMap.addAttribute("articleCount", articleCount);*/
		if(articleType.equals(ArticleType.AT001.getCode())) {
			return "front/article/articlePage";
		}else{
			return "front/article/gg";
		}
		
	}
	
	/**  
	* @Title: pageList  
	* @Description: (这里用一句话描述这个方法的作用)  
	* @param @param request
	* @param @param response
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws  
	*/ 
	@RequestMapping("/articlePageList")
	@ResponseBody
	public Map<String, Object> articlePageList(HttpServletRequest request, HttpServletResponse response,Integer currentPage,Integer pageNum,
			Integer articleType,Integer articleStatus){
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		
		try {
			if(currentPage==null) {
				currentPage = this.currentPage;
			}
			if(pageNum==null) {
				pageNum = this.pageNum;
			}
			if(articleStatus==null){
				articleStatus = 1;
			}
			accessMap.put("articleType", articleType);
			accessMap.put("articleStatus", articleStatus);
			accessMap.put("currentResult", (currentPage-1)*pageNum);
			accessMap.put("endResult", pageNum);
			Integer articleCount = articleService.findArticleCountByCondition(accessMap);
			List<ArticleVO> articleList = articleService.pageList(accessMap);
			viewMap.put("articleList", articleList);
			viewMap.put("articleCount", articleCount);
			viewMap.put("code", 1);
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "操作有误");
		}
		return viewMap;
	}

	/**
	 * 文章详情页
	 * @param articleId
	 * @param articleType
	 * @return
	 */
	@RequestMapping("articleDetail/{articleId}")
	public String articleDetail(HttpServletRequest request,@PathVariable("articleId") Long articleId,Integer articleType,
			ModelMap modelMap) {
		Map<String, Object> accessMap = new HashMap<>();
		if(articleId!=null) {
			accessMap.put("articleId", articleId);
			accessMap.put("articleType", articleType);
			ArticleVO article = articleService.findArticleById(articleId);
			//前一篇
			ArticleVO aheadArticle = articleService.findAheadArticle(accessMap);
			//后一篇
			ArticleVO afterArticle = articleService.findAfterArticle(accessMap);
			modelMap.addAttribute("article", article);
			modelMap.addAttribute("aheadArticle", aheadArticle);
			modelMap.addAttribute("afterArticle", afterArticle);
		}
		return "front/article/articleDetail";
	}
	
}
